<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 2016/11/9
 * Time: 上午11:52
 */
class ModActivity {

    public static function indexData($params){
        $table = 'tblGameActivity';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server AND ActivityType<>"ALL" AND ActivityID<>"ALL" ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        if(!empty($params['act_type'])){
            $where .= ' AND ActivityType=:act_type ';
            $args[':act_type'] = $params['act_type'];
        }

        $sql = "SELECT ActivityType,ActivityId,ActivityName,SUM(GetNumber) AS GetNumber,
                  SUM(GetCnt) AS GetCnt,SUM(Money) AS Money,SUM(Mage) AS Mage,
                  SUM(ItemNumber) AS ItemNumber,SUM(GiftNumber) AS GiftNumber,
                  SUM(ActProcNum) AS ActProcNum,SUM(ActProcCnt) AS ActProcCnt,
                  SUM(ActCurNum) AS ActCurNum,SUM(ActTargetNum) AS ActTargetNum
                FROM {$table} {$where} GROUP BY ActivityType,ActivityId,ActivityName";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function itemData($params){
        $table = 'tblGameActItem';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server AND ActivityType<>"ALL" ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        if(!empty($params['act_type'])){
            $where .= ' AND ActivityType=:act_type ';
            $args[':act_type'] = $params['act_type'];
        }

        if(!empty($params['act_id'])){
            $where .= ' AND ActivityId=:act_id ';
            $args[':act_id'] = $params['act_id'];
        }

        $sql = "SELECT ItemId,SUM(GetNumber) AS GetNumber
                FROM {$table} {$where} GROUP BY ItemId";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function giftData($params){
        $table = 'tblGameActGift';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server AND ActivityType<>"ALL" ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        if(!empty($params['act_type'])){
            $where .= ' AND ActivityType=:act_type ';
            $args[':act_type'] = $params['act_type'];
        }

        if(!empty($params['act_id'])){
            $where .= ' AND ActivityId=:act_id ';
            $args[':act_id'] = $params['act_id'];
        }

        $sql = "SELECT GiftId,SUM(GetNumber) AS GetNumber
                FROM {$table} {$where} GROUP BY GiftId";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function finishedData($params){
        $table = 'tblGameActivity';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period=:period
                        AND Platform=:platform AND GameSrc=:game AND SID=:server AND ActivityType<>"ALL" AND ActivityID<>"ALL" ';

        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        if(!empty($params['act_type'])){
            $where .= ' AND ActivityType=:act_type ';
            $args[':act_type'] = $params['act_type'];
        }

        $sql = "SELECT ActivityType,ActivityId,ActivityName,
                  SUM(ActCurNum) AS ActCurNum,SUM(ActTargetNum) AS ActTargetNum,
                  SUM(ActProcNum) AS ActProcNum,SUM(ActProcCnt) AS ActProcCnt,
                  SUM(ActCompNum) AS ActCompNum,SUM(ActTargetUsers) AS ActTargetUsers
                FROM {$table} {$where} GROUP BY ActivityType,ActivityId,ActivityName";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }
}